﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class TreachersGradesBll : ITreachersGradesBll
    {
        private ITreachersGradesDal _dal;
        public TreachersGradesBll(ITreachersGradesDal dal)
        {
            _dal = dal;
        }

        public async Task<int> AddTreachersGradesAsync(Guid gradesId, Guid teachersId, DateTime createTime)
        {
            return await _dal.AddAsync(new Model.TeachersGrades 
            {
                GradesId = gradesId,
                TeachersId = teachersId,
                CreateTime = createTime
            });
        }

        public async Task<int> DeleteTreachersGradesAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<TreachersGradesDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(tg => new TreachersGradesDto
            {
                Id = tg.Id,
                GradesId=tg.GradesId,
                TeachersId=tg.TeachersId,
                CreateTime=tg.CreateTime
            }).ToList();
        }

        public async Task<TreachersGradesDto> GetTreachersGradesByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return new TreachersGradesDto 
            {
                Id=data.Id,
                GradesId = data.GradesId,
                TeachersId=data.TeachersId,
                CreateTime= DateTime.Now
            };
        }

        public async Task<List<TreachersGradesDto>> GetTreachersGradesByNameAsync(string title)
        {
            //var data = await _dal.GetDataByChoose(tg => tg.TeachersId.Contains(title));
            return null;
        }

        public async Task<int> UpdateTreachersGradesAsync(Guid id, Guid gradesId, Guid teachersId, DateTime updateTime)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            gradesId = data.GradesId;
            teachersId = data.TeachersId;
            updateTime = DateTime.Now;
            return await _dal.UpdateAsync(data);
        }
    }
}
